package com.flying.srccode;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

/**
 * @ClassName VectorSrc
 * @Author flying
 * @Date 2025/2/1 21:19
 * @Description Vector源码测试
 */
public class VectorSrc {


    /**
     *  Vector源码： 与ArrayList的区别
     *  1. 源码中大量使用synchronized 关键字，用于线程的同步
     *  2. ArrayList使用延迟初始化；Vector使用立即初始化容量10
     *  3. ArrayList扩容规则为1.5倍；Vector扩容规则2倍
     */
    @Test
    void test1(){


        /**
         *     public Vector() {
         *         this(10);   // 内部调用重载构造器
         *     }
         *
         *
         *     public Vector(int initialCapacity) {
         *         this(initialCapacity, 0);    this(10,0)
         *     }
         *
         *     public Vector(int initialCapacity, int capacityIncrement) {
         *         super();
         *         if (initialCapacity < 0)
         *             throw new IllegalArgumentException("Illegal Capacity: "+
         *                                                initialCapacity);
         *         this.elementData = new Object[initialCapacity];    //  this.elementData=new Object[10];
         *         this.capacityIncrement = capacityIncrement;        // this.capacityIncrement = 0
         *     }
         */

        // 立即初始化--- new时，直接将容器的容量初始化为  10
        Vector<String>  vector=new Vector<>();


        /**
         *     public synchronized boolean add(E e) {
         *         modCount++;
         *         add(e, elementData, elementCount);   // add("a", elementData, 0);
         *         return true;
         *     }
         *
         *     private void add(E e, Object[] elementData, int s) {
         *         if (s == elementData.length)  // 判断容器内元素个数是否等于容器的长度，（此时的元素能否添加到容器中)
         *             elementData = grow(); // 扩容
         *         elementData[s] = e;  //
         *         elementCount = s + 1;  // 记录容器中的元素个数
         *
         *     // 需要扩容时：
         *     private Object[] grow() {
         *         return grow(elementCount + 1);      // grow(10+1)
         *     }
         *
         *
         *    private Object[] grow(int minCapacity) {   10
         *         int oldCapacity = elementData.length;    // 10
         *         int newCapacity = ArraysSupport.newLength(
         *         oldCapacity,   //  10
         *         minCapacity - oldCapacity,  11-10 --> 1
         *         capacityIncrement > 0 ? capacityIncrement : oldCapacity   -->   10
         *         );
         *         return elementData = Arrays.copyOf(elementData, newCapacity);  // 扩容为20
         *}
         *
         *    int prefLength = oldLength + Math.max(minGrowth, prefGrowth); // might overflow
         *         if (0 < prefLength && prefLength <= SOFT_MAX _ARRAY_LENGTH) {
         *             return prefLength;  //   prefLength=10+10 --> 20
         *         } else {
         *             // put code cold in a separate method
         *             return hugeLength(oldLength, minGrowth);
         *         }
         *
         */
        vector.add("a");




    }




}
